<template>
  <div class="app-container">
    <div class="addbtn">
      <el-upload
        ref="upload"
        class="upload-demo"
        :action="pathUrl+'/file/doc'"
        :data="paramsData"
        :with-credentials="true"
        :show-file-list="false"
        :on-change=" beforeUpimg"
        :on-success="successData"
      >
        文件上传
      </el-upload>
    </div>
    <div class="seatch">
      <el-input
        v-model="listQuery.keyword"
        placeholder="按名称搜索"
        @keyup.enter.native="searchName($event)"
      >
        <i slot="prefix" class="el-input__icon el-icon-search" @click="getList()" />
      </el-input>
    </div>
    <div class="content">
      <el-table
        v-loading="listLoading"
        align="center"
        :data="tableData"
        style="width: 100%"
      >
        <el-table-column
          prop="id"
          align="center"
          label="序号"
        />
        <el-table-column
          prop="name"
          align="center"
          label="文件名称"
        />
        <el-table-column
          align="center"
          prop="createAt"
          label="上传时间"
        />
        <el-table-column
          align="center"
          prop="createAt"
          label="操作"
          width="220"
        >
          <template slot-scope="{row}">
            <div class="operate">
              <div v-permission="['directory:delete']" @click="deleteDialog(row)">删除</div>
              <!-- <div @click="downDialog(row)">下载</div> -->
              <div @click="downFile(row)">下载
                <!-- <a :href="pathUrl+'/file/'+row.id">下载</a> -->
              </div>
              <div @click="shareDialog(row)">分享</div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="pageination-div">
        <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="getList" />
      </div>

    </div>

    <!-- 分享 -->
    <el-dialog title="分享" :visible.sync="dialogFormVisible" :close-on-click-modal="false" custom-class="customClass">
      <el-form ref="addForm" :model="addForm" :rules="rules" auto-complete="on" label-position="left" style="width: 97%;">
        <el-form-item label="分享链接" :label-width="formLabelWidth" prop="path">
          <el-input
            v-model="addForm.path"
            placeholder="请输入名称"
            name="path"
            tabindex="1"
            autocomplete="off"
            :readonly="true"
          />
        </el-form-item>
        <el-form-item label="复制链接" :label-width="formLabelWidth">
          <div
            class="btnSub"
            :data-clipboard-text="addForm.path"
            @click="copyLink"
          >复制链接</div>
        </el-form-item>
      </el-form>
      <div class="footer">
        <div class="cancel" @click="dialogFormVisible=false">
          <span>取消</span>
        </div>
      </div>
    </el-dialog>

    <delete-dialog
      dislog-name="文件"
      :dialog-del-visible="dialogDelVisible"
      :title="'文件'"
      @funcancl="funcancl"
      @func="deleteData"
    />
  </div>

</template>

<script>
import Clipboard from 'clipboard'
import Pagination from '@/components/pagination'
import DeleteDialog from '@/components/dialog/delete'
import { getFileList, shareFile } from '@/api/directory'
import { deleteFiles } from '@/api/file'
export default {
  name: 'OrganizationUser',
  components: {
    Pagination,
    DeleteDialog
  },
  props: {
    teamId: {
      type: Number,
      default: null
    },
    teamObject: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      pathUrl: process.env.VUE_APP_BASE_API,
      paramsData: {
        directoryId: ''
      },
      addForm: {
        path: ''
      },
      tableData: [],
      total: 0,
      listLoading: true,
      listQuery: {
        directoryId: this.teamId,
        keyword: null,
        pageNum: 1,
        pageSize: 10
      },
      dialogDelVisible: false,
      dialogFormVisible: false,
      formLabelWidth: '120px',
      lablePosition: 'left',
      teamName: '',
      deleteObiect: {},
      rules: {}
    }
  },
  watch: {
    teamId: function(value) {
      this.listQuery.directoryId = value
      this.paramsData.directoryId = value
      this.getList()
    },
    teamObject: function(param) {
      this.teamName = param.name
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    sizeChange(val) {
      this.pageNum = val
    },
    currentChange(val) {
      this.currentPage = val
    },
    searchName(e) {
      this.listQuery.keyword = e.target.value
      this.getList()
    },
    funcancl(val) {
      this.dialogDelVisible = false
    },
    downFile(row) {
      var url = this.pathUrl + '/file/' + row.id
      window.open(url)
    },
    // 获取数据
    async getList() {
      const res = await getFileList(this.listQuery)
      if (res.code === 200) {
        this.tableData = res.data.list
        this.total = res.data.total || 0
        this.pages = res.data.pages || 0
        this.listLoading = false
      }
    },
    // 删除
    deleteDialog(row) {
      this.dialogDelVisible = true
      this.deleteObiect = row
    },
    async deleteData(row) {
      const id = this.deleteObiect.id
      const res = await deleteFiles(id)
      if (res.code === 200) {
        this.getList()
        this.dialogDelVisible = false
        this.$message({
          type: 'success',
          message: '删除成功!',
          showClose: true,
          center: true,
          duration: 2 * 1000
        })
      } else {
        this.dialogDelVisible = false
      }
    },
    beforeUpimg(file, fileList) {
      // console.log(file)
    },
    successData(response, file, fileList) {
      if (response.code === 200) {
        this.$message({
          type: 'success',
          message: '文件上传成功!',
          showClose: true,
          center: true,
          duration: 2 * 1000
        })
        this.getList()
      } else {
        this.$refs.upload.clearFiles()
        this.$message({
          type: 'error',
          message: '文件上传失败!',
          showClose: true,
          center: true,
          duration: 2 * 1000
        })
      }
    },
    // 分享
    async shareDialog(row) {
      const res = await shareFile(row.id)
      if (res.code === 200) {
        this.addForm.path = res.data
        this.dialogFormVisible = true
      }
    },
    copyLink() {
      var clipboard = new Clipboard('.btnSub')
      clipboard.on('success', e => {
        console.log('复制成功')
        // 释放内存
        clipboard.destroy()
      })
      clipboard.on('error', e => {
        // 不支持复制
        console.log('该浏览器不支持自动复制')
        // 释放内存
        clipboard.destroy()
      })
      this.$message({
        message: '复制成功',
        type: 'success'
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.app-container{
  margin: 0;
  min-height: 700px;
  .seatch{
    right: 20px;
  }
  .textName{
    font-size:14px;
    font-weight:400;
    color:rgba(247,181,0,0.8);
  }
}
</style>
